-- AccountM
-- created by panyl
-- 账号模块

module("AccountM", package.seeall);

-- 绑定账号
local mBindAccounts = {};

-- 当前登录游戏中的账号
local mGamingAccount = nil;

-- 模块初始化操作
function init()
    mGamingAccount = nil;
    mBindAccounts = {};
end

-- 检查账号合法性
function checkAccount(account)
    return true
end

-- 去除账号头尾空格
function removeSpace(account)
    local retAccount = account;

    local index = string.find(retAccount, " ", 1)

    -- 去掉头部所有空格
    while index and index == 1 do
        retAccount = string.gsub(retAccount, " ", "", 1);
        index = string.find(retAccount, " ", 1);
    end

    index = string.find(retAccount, " ", string.len(retAccount))

    -- 去掉尾部所有空格
    while index and index == string.len(retAccount) do
        retAccount = string.sub(retAccount, 1, string.len(retAccount) - 1);
        index = string.find(retAccount, " ", string.len(retAccount))
    end

    return retAccount;
end

-- 去除账号首尾的空格和tab
function removeTabAndSpace(account)
    return string.match(account, "^[　%s]*(.-)[　%s]*$");
end

-- 通知服务器客户端的焦点变化
function noticeFocusChange(focusList)
    -- 啥都不干，后续考虑放到talkingdata去统计
    return true;
end

-- 设置绑定账号
function setBindAccounts(accounts)
    printTable(accounts, "当前绑定账号");
    mBindAccounts = accounts;
end

-- 设置绑定账号
function setBindAccount(account)
    table.insert(mBindAccounts, account);
end

-- 获取绑定账号
function getBindAccounts()
    return mBindAccounts;
end

-- 设置当前登录游戏的账号
function setGamingAccount(account)
    mGamingAccount = account;
end

-- 获取游戏中的账号
function getGamingAccount()
    return mGamingAccount;
end

-- 获取游戏中账号的渠道
function getGamingAccountChannel()
    local account = getGamingAccount();
    if not account then
        return nil;
    end

    local index = string.find(account, "_");
    return string.sub(account, 1, index - 1);
end

-- 计算账号绑定奖励
function calcBindAccountBonus()
    return FormulaM.invoke("CALC_BIND_ACCOUNT_BONUS");
end

-- 能否领取账号绑定奖励
function canTakeAccountBindBonus()
    local accounts = getBindAccounts();
    if #accounts == 0 then
        --trace("AccountM", "没有绑定账号，无法领取奖励。");
        return false;
    end

    if ME.user.dbase:query("account_bind_rewarded") == 1 then
        trace("AccountM", "已经领取过账号绑定奖励了，不能重复领取。");
        return false;
    end

    return true;
end

-- 是否领取过账号绑定奖励
function hasTakenBindBonus()
    return ME.user.dbase:query("account_bind_rewarded") == 1;
end

-- 领取账号绑定奖励
function takeAccountBindBonus()
    local accounts = getBindAccounts();
    if #accounts == 0 then
        trace("AccountM", "没有绑定账号，无法领取奖励。");
        return false;
    end

    if hasTakenBindBonus() then
        trace("AccountM", "已经领取过账号绑定奖励了，不能重复领取。");
        return false;
    end

    -- 标记已领取
    ME.user.dbase:set("account_bind_rewarded", 1);

    -- 执行奖励
    local bonus = calcBindAccountBonus();
    BonusM.doBonus(bonus, "account_bind");

    EventMgr.fire(event.ACCOUNT_BIND_BONUS, { ["bonus"] = bonus, ["account"] = accounts[1],});

    return true;
end

-- 获取可领取的奖励个数
function getAvailableBonusNum()
    local num = 0;
    if true == canTakeAccountBindBonus() then
        -- 可领取账号绑定奖励
        num = num + 1;
    end
    return num;
end

-- 获取可绑定的账号渠道
function getChannelsCanBind()
    return FormulaM.invoke("GET_CHANNELS_CAN_BIND");
end

-- 是否开放账号绑定功能
function isAccountBindOpen()
    if isOverseaPackage() or PUBLISH_PLATFORM == PUBLISH_TYPE_APP_STORE_CN then
        return true;
    end

    return false;
end

-- 注册青瓷账号
function registerQCAccount(account, password)
    assert(type(account) == "string", "账号必须为字符串");
    assert(type(password) == "string", "密码必须为字符串");

    -- 注册地址
    local url = "http://account.qcplay.com/AppLogin/quickreg";

    -- 构建参数
    local params = string.format("?account=%s&password=%s&id=%d",
        account, password, QC_GAME_ID);
    url = url .. params;

    trace("AccountM", "registerQCAccount : url = %s", url);

    local function callback(responseData)
        trace("AccountM", "responseData = %s", tostring(responseData));
        local output = string.parseJsonToTable(responseData);
        EventMgr.fire(event.QC_REGISTER_RESULT, output);
    end

    -- 发送 http get 请求
    getRemoteHostData(url, callback);
end
